package com.juzi.wopu.web;

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.juzi.wopu.domain.CanteenMenu;
import com.juzi.wopu.domain.CanteenMenuResponse;
import com.juzi.wopu.domain.SysCode;
import com.juzi.wopu.query.CanteenMenuQuery;
import com.juzi.wopu.query.PageList;
import com.juzi.wopu.query.SysCodeQuery;
import com.juzi.wopu.service.ICanteenMenuService;
import com.juzi.wopu.service.IImageService;
import com.juzi.wopu.service.ISysCodeService;
import com.juzi.wopu.service.impl.IImageServiceImpl;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

public class CanteenMenuAction extends CRUDAaction<CanteenMenu> {
	
	private static final long serialVersionUID = 1L;

	private ICanteenMenuService canteenMenuService;

	private ISysCodeService sysCodeService;
	
	private IImageService imageService=new IImageServiceImpl();
	
	private CanteenMenu canteenMenu = new CanteenMenu();
	// 实例化一个查询对象
	private CanteenMenuQuery baseQuery = new CanteenMenuQuery();
	
	private SysCodeQuery sysCodeQuery = new SysCodeQuery();
	// 当前类对象 是栈顶对象
	private PageList pageList;
	
    private List<CanteenMenu> data=new ArrayList<>();
	
	public List<CanteenMenu> getData() {
		return data;
	}

	public void setData(List<CanteenMenu> data) {
		this.data = data;
	}
	
	private File upload;
	private String contentType;
	private String fileName;
	
	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}
	
	public String getUploadContentType() {
		return contentType;
	}

	public void setUploadContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getUploadFileName() {
		return fileName;
	}

	public void setUploadFileName(String fileName) {
		this.fileName = fileName;
	}

	public void setCanteenMenuService(ICanteenMenuService canteenMenuService) {
		this.canteenMenuService = canteenMenuService;
	}
	
	public void setSysCodeService(ISysCodeService sysCodeService) {
		this.sysCodeService = sysCodeService;
	}
	
	@Override
	protected void list() {
		this.pageList = canteenMenuService.findByQuery(baseQuery);
		List<SysCode> list=sysCodeService.getAll();
		List<SysCode> allSyscode=new ArrayList<>();
		if(list!=null && list.size()>0){
			for(SysCode sysCode:list){
				if((sysCode.getiType()!=null && sysCode.getiType()==10L) && (sysCode.getiParentId()==null || sysCode.getiParentId()==910)){
					allSyscode.add(sysCode);
				}
			}
		}
		
		if(pageList!=null && pageList.getRows()!=null && pageList.getRows().size()>0){
			@SuppressWarnings("unchecked")
			List<Object> rows=pageList.getRows();
			for(Object pro:rows){
				CanteenMenuResponse response=new CanteenMenuResponse((CanteenMenu)pro);
				String imgPath=imageService.downloadSmallImg(((CanteenMenu)pro).getsImgPath());
				response.setSmallImgPath(imgPath);
				data.add(response);
			}
		}
		putContext("allSyscode", allSyscode);
	}

	@Override
	public String input() throws Exception {
		List<SysCode> list=sysCodeService.getAll();
		List<SysCode> allSyscode=new ArrayList<>();
		if(list!=null && list.size()>0){
			for(SysCode sysCode:list){
				if((sysCode.getiType()!=null && sysCode.getiType()==10L) && (sysCode.getiParentId()==null || sysCode.getiParentId()==910)){
					allSyscode.add(sysCode);
				}
			}
		}
		putContext("allSyscode", allSyscode);
		return INPUT;
	}

	// 做非空，长度，格式
	public void validateSave() {
		if (StringUtils.isBlank(canteenMenu.getsName())) {
			addFieldError("name", "名称不能为空");
		}
	}

	// 处理保存和修改方法，你这个命名要规范
	@Override
    @InputConfig(methodName="input")
	public String save() throws Exception {
		
		SysCode sysCode = canteenMenu.getSysCode();
		if(sysCode != null && sysCode.getiId() == -1L){
			canteenMenu.setSysCode(null);
		}
		//  新增后的保存
		if(id==null){
			if(upload!=null){
				String imgPath=imageService.upload(upload, canteenMenu.getiType()+"");
				canteenMenu.setsImgPath(imgPath);
			}else {
				canteenMenu.setsImgPath("");
			}
			// 保存后添加到最后一页
			baseQuery.setCurrentPage(Integer.MAX_VALUE);
		}
		else {
			if(upload!=null){
				String imgPath=imageService.upload(upload, canteenMenu.getiType()+"");
				canteenMenu.setsImgPath(imgPath);
			}
		}
		
		canteenMenuService.save(canteenMenu);
		return RELOAD;
	}

	// 删除
	@Override
	public String delete() throws Exception {
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {
			if (id != null) {
				canteenMenuService.delete(id);
				out.print("{\"success\":true,\"msg\":\"删除成功\"}");
			}else{
				out.print("{\"success\":false,\"msg\":\"没有删除成功\"}");
			}
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"success\":false,\"msg\":\"异常信息:"+e.getMessage()+"\"}");
		}
		return null;
	}

	// input前置方法
	@Override
	public void prepareInput() throws Exception {
		if (id != null) {// 最主要做回显
			canteenMenu = canteenMenuService.get(id);// 变成栈顶对象
		}
	}

	// save前置方法
	@Override
	public void prepareSave() throws Exception {
		if (id == null) {
			// 新增后的保存
			canteenMenu = new CanteenMenu();
		} else {
			canteenMenu = canteenMenuService.get(id);
			canteenMenu.setSysCode(null);
		}
	}

	// 圧栈后院获取
	@Override
	public CanteenMenu getModel() {
		return canteenMenu;
	}

	public PageList getPageList() {
		return pageList;
	}

	public CanteenMenuQuery getBaseQuery() {
		return baseQuery;
	}

	public void setBaseQuery(CanteenMenuQuery baseQuery) {
		this.baseQuery = baseQuery;
	}

	public SysCodeQuery getSysCodeQuery() {
		return sysCodeQuery;
	}

	public void setSysCodeQuery(SysCodeQuery sysCodeQuery) {
		this.sysCodeQuery = sysCodeQuery;
	}
}
